package com.youhome.business.controller;

import com.youhome.business.dto.DTOClientRegister;
import com.youhome.business.dto.DTOClientSearch;
import com.youhome.business.dto.DTOClientUpdate;
import com.youhome.business.dto.DTOCompanyClientSearch;
import com.youhome.common.exception.CustomException;
import com.youhome.common.response.Result;
import com.youhome.common.response.ResultCode;
import com.youhome.common.response.ResultPage;
import com.youhome.service.model.BClient;
import com.youhome.service.model.Principal;
import com.youhome.service.parameter.ParamClientRegister;
import com.youhome.service.parameter.ParamCompanyClientSearch;
import com.youhome.service.service.BClientService;
import io.swagger.annotations.*;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * Created by chen365 on 2017/10/25.
 */
@RestController
@RequestMapping(value = "/client")
@Api(tags = "客户")
public class ClientController {
    @Autowired
    private BClientService bClientService;


    @ApiOperation(value = "客户列表", notes = "客户列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/list")
    public Object list(@ApiParam @Validated DTOClientSearch dtoClientSearch, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new ResultPage(ResultCode.SUCCESS, bClientService.selectByBean(principal, dtoClientSearch.getPageNumber(), dtoClientSearch.getPageSize(), dtoClientSearch.getKeyword(), modelMapper.map(dtoClientSearch, BClient.class)));
    }

    @ApiOperation(value = "导出客户列表", notes = "导出客户列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/export")
    public Object export(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, bClientService.export(principal));
    }

    @ApiOperation(value = "公司客户列表", notes = "公司下所有顾问的所有客户列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/company/list")
    public Object companyList(@ApiParam @Validated DTOCompanyClientSearch dtoCompanyClientSearch, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {

        ModelMapper modelMapper = new ModelMapper();
        return new ResultPage(ResultCode.SUCCESS,
                bClientService.selectCompanyClientByBean(principal, modelMapper.map(dtoCompanyClientSearch, ParamCompanyClientSearch.class)));

    }

    @ApiOperation(value = "客户注册", notes = "客户注册", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = "/signin")
    public Object signin(@ApiParam @Validated DTOClientRegister dtoClientRegister) throws CustomException {
        ModelMapper modelMapper = new ModelMapper();
        return new Result<>(ResultCode.SUCCESS, bClientService.register(modelMapper.map(dtoClientRegister, ParamClientRegister.class)));
    }

    @ApiOperation(value = "导出公司下客户列表", notes = "导出公司下所有客户列表", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/company/export")
    public Object companyExport(@ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, bClientService.companyExport(principal));
    }

    @ApiOperation(value = "客户详情", notes = "客户详情", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @GetMapping(value = "/detail/{clientId}")
    public Object detail(@PathVariable Long clientId) throws CustomException {
        return new Result<>(ResultCode.SUCCESS, bClientService.selectByPrimaryKey(clientId));
    }

    @ApiOperation(value = "修改客户", notes = "修改客户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PutMapping(value = "/update")
    public Object update(@ApiParam @Validated DTOClientUpdate dtoClientUpdate) {
        ModelMapper modelMapper = new ModelMapper();
        return new Result(ResultCode.SUCCESS, bClientService.update(modelMapper.map(dtoClientUpdate, BClient.class)));
    }

    @ApiOperation(value = "删除客户", notes = "状态删除客户", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @DeleteMapping(value = "/delete/{clientId}")  //提醒前端修改
    public Object delete(@PathVariable Long clientId, @ApiIgnore @AuthenticationPrincipal Principal principal) throws CustomException {
        return new Result(ResultCode.SUCCESS, bClientService.deleteByClientId(principal, clientId));
    }

}
